package com.kede.app.rest;

import com.kede.annotation.Log;
import com.kede.app.domain.LeescoreOrder;
import com.kede.app.service.LeescoreOrderService;
import com.kede.app.service.dto.LeescoreOrderQueryCriteria;
import com.kede.dictenum.OrderStatusDictEnum;
import com.kede.dictenum.PayStatuDictEnum;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
* @author Pr.li
* @date 2023-05-12
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "leescoreOrder管理")
@RequestMapping("/api/leescoreOrder")
public class LeescoreOrderController {

    private final LeescoreOrderService leescoreOrderService;

    /**
     * 导出数据
     **/
    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('leescoreOrder:list')")
    public void exportLeescoreOrder(HttpServletResponse response, LeescoreOrderQueryCriteria criteria) throws IOException {

        leescoreOrderService.download(leescoreOrderService.queryAll(criteria), response);
    }

    /**
    * 查询leescoreOrder
    **/
    @GetMapping
    @Log("查询leescoreOrder")
    @ApiOperation("查询leescoreOrder")
    @PreAuthorize("@el.check('leescoreOrder:list')")
    public ResponseEntity<Object> queryLeescoreOrder(LeescoreOrderQueryCriteria criteria, Pageable pageable){
        if (criteria.getPay()!=null){
            PayStatuDictEnum payStatuDictEnum = PayStatuDictEnum.valueOfCode(criteria.getPay());
            criteria.setPay(payStatuDictEnum.getEnumIndex());//转成下标
        }
        if(criteria.getStatus()!=null){
            OrderStatusDictEnum orderStatusDictEnum = OrderStatusDictEnum.valueOfCode(criteria.getStatus());
            criteria.setStatus(orderStatusDictEnum.getEnumIndex());//转成下标
        }
        return new ResponseEntity<>(leescoreOrderService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    /**
    * 新增leescoreOrder
    **/
    @PostMapping
    @Log("新增leescoreOrder")
    @ApiOperation("新增leescoreOrder")
    @PreAuthorize("@el.check('leescoreOrder:add')")
    public ResponseEntity<Object> createLeescoreOrder(@Validated @RequestBody LeescoreOrder resources){
        return new ResponseEntity<>(leescoreOrderService.create(resources),HttpStatus.CREATED);
    }


    /**
    * 修改leescoreOrder
    **/
    @PutMapping
    @Log("修改leescoreOrder")
    @ApiOperation("修改leescoreOrder")
    @PreAuthorize("@el.check('leescoreOrder:edit')")
    public ResponseEntity<Object> updateLeescoreOrder(@Validated @RequestBody LeescoreOrder resources){
        leescoreOrderService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }


    /**
    * 删除leescoreOrder
    **/
    @DeleteMapping
    @Log("删除leescoreOrder")
    @ApiOperation("删除leescoreOrder")
    @PreAuthorize("@el.check('leescoreOrder:del')")
    public ResponseEntity<Object> deleteLeescoreOrder(@RequestBody Integer[] ids) {
        leescoreOrderService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}